package com.interactive.internal.ant;

import java.io.File;
import java.util.Iterator;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.types.Reference;

/**
 *
 * This custom ant taks allows users to generate multiple jar files in a single
 * task.  BatchJar also allows {@link JarSet JarSets} to be defined and passed
 * to the BatchJar task with a reference id.
 *
 * <p>Title: Ant Batch Jar</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Interactive Network Technologies Inc.</p>
 *
 * @author John Januskey (john@int.com)
 * @version 1.0
 *
 *
 * Usage:
 * <code>
 *   <path id="task.path">
 *      <!-- int.ant.jar.dir is the director containing the INT-Ant.jar file
 *           which contains custom ant tasks defined for the INT build system.-->
 *      <fileset dir="int.ant.jar.dir" includes="*.jar"/>
 *   </path>
 *
 *   <!-- the loaderref attribute is required to ensure that the same
 *        classloader is used for both the custom typedef and the taskdef.
 *        If the loaderref attribute is not specified it will cause a
 *        ClassCastException when trying to use a predefined jarset by
 *        refid. -->
 *   <typedef name="jarset" classname="com.interactive.internal.ant.tasks.JarSet" classpathref="task.path" loaderref="jarset"/>
 *   <taskdef name="batchJar" classname="com.interactive.internal.ant.tasks.BatchJar" classpathref="task.path" loaderref="jarset"/>
 *
 *   <!-- create a jarset with the refid project.jars
 *   <jarset id="project.jars">
 *
 *     <!-- add a jar called one.jar with a fileset containing all files in the
 *          current directory -->
 *     <jarelement name="one.jar">
 *       <fileset dir="./">
 *         <include name="*.*"/>
 *       </fileset>
 *     </jarelement>
 *
 *     <!-- add a jar called two.jar with a fileset containing only the class
 *          files in the current directory -->
 *     <jarelement name="two.jar">
 *       <fileset dir="./">
 *         <include name="*.class"/>
 *       </fileset>
 *     </jarelement>
 *   </jarset>
 *
 *   <!-- define a task to creat the jar files using the previously defined
 *        jarset. -->
 *   <target name="makeJars">
 *     <!-- delete the old versions -->
 *     <delete file="one.jar"/>
 *     <delete file="two.jar"/>
 *
 *     <!-- call the batchJar task with project.jars -->
 *     <batchJar jarsetref="project.jars"/>
 *   </target>
 * </code>
 *
 *
 */
public class BatchJar
    extends Task
{
  private JarSet jarSet;

  public JarSet createJarSet()
  {
    jarSet = new JarSet();
    jarSet.setProject(getProject());
    return jarSet;
  }

  public void setJarSetRef(Reference r)
  {
    createJarSet().setRefid(r);
  }

  public void execute()
  {
    Iterator itr = jarSet.getJarSetElementsList().iterator();
    PatternSet archiveTargets = new PatternSet();
    while (itr.hasNext())
    {
      Jar jar = (Jar) itr.next();
      jar.execute();
      archiveTargets.createInclude().setName( getJarName(jar));
    }
    getProject().addReference( "archive.targets", archiveTargets );
  }

  private String getJarDir( Jar jar )
  {
    String fullPath = jar.getDestFile().getAbsolutePath();
    int lastSlash = fullPath.lastIndexOf( File.separator );
    return fullPath.substring( 0, lastSlash );
  }

  private String getJarName( Jar jar )
  {
    String fullPath = jar.getDestFile().getAbsolutePath();
    int lastSlash = fullPath.lastIndexOf( File.separator );
    return fullPath.substring( lastSlash + 1 );
  }
}
